package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.qixi.UserQixiData;
import com.yanqu.road.entity.qixi.UserQixiGiftData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserQixiGiftDaoImpl extends TempDataObjectDao<UserQixiGiftData> {
    public static String getUserQixiGiftDataSql = "select * from `t_u_qixi_gift` where `activityId`=? and `userId`=? and `status`=0";

    public List<UserQixiGiftData> getUserQixiGiftDataList(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserQixiGiftData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserQixiGiftDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserQixiGiftData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public Map<Long, List<UserQixiGiftData>> getNeedSendMailGift(int activityId) {
        String sql = "select * from `t_u_qixi_gift` where `activityId`=? and `status`=0";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<UserQixiGiftData>> dataMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserQixiGiftData data = parse(rs);
                List<UserQixiGiftData> list = dataMap.get(data.getUserId());
                if (list == null) {
                    list = new ArrayList<>();
                    dataMap.put(data.getUserId(), list);
                }
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserQixiGiftDataSql = "update `t_u_qixi_gift` set targetUserId=?,flowerId=?,flowerCount=?,giftTime=?,content=?,status=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserQixiGiftData userQixiGiftData) {
        String sql = "update `t_u_qixi_gift` set status=? where activityId=? and userId=? and targetUserId=? and giftTime=?";
        userQixiGiftData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userQixiGiftData.getStatus()));

        para.put(i++, new DbParameter(Types.INTEGER, userQixiGiftData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiGiftData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiGiftData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiGiftData.getGiftTime()));

        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserQixiGiftDataSql = "insert into `t_u_qixi_gift`(activityId,userId,targetUserId,flowerId,flowerCount,giftTime,content,status) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserQixiGiftData userQixiGiftData) {
        userQixiGiftData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userQixiGiftData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiGiftData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiGiftData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiGiftData.getFlowerId()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiGiftData.getFlowerCount()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiGiftData.getGiftTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userQixiGiftData.getContent()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiGiftData.getStatus()));
        if (getDb().executeNoneQuery(insertUserQixiGiftDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserQixiGiftData parseData(ResultSet rs) throws SQLException {
        UserQixiGiftData userQixiGiftData = new UserQixiGiftData();
        userQixiGiftData.setActivityId(rs.getInt("activityId"));
        userQixiGiftData.setUserId(rs.getLong("userId"));
        userQixiGiftData.setTargetUserId(rs.getLong("targetUserId"));
        userQixiGiftData.setFlowerId(rs.getInt("flowerId"));
        userQixiGiftData.setFlowerCount(rs.getInt("flowerCount"));
        userQixiGiftData.setGiftTime(rs.getLong("giftTime"));
        userQixiGiftData.setContent(rs.getString("content"));
        userQixiGiftData.setStatus(rs.getInt("status"));

        return userQixiGiftData;
    }
}
